package com.dylan.历史.zookeeper;

import com.dylan.历史.util.LogUtil;
import com.dylan.历史.util.SleepUtils;
import org.apache.zookeeper.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.CountDownLatch;

/**
 * Created by Dylan on 2018/11/6.
 */
public class 创建节点 {

    private static CountDownLatch countDownLatch = new CountDownLatch(1);

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        ZooKeeper zookeeper = new ZooKeeper("192.144.128.229:2181,192.144.128.229:2182,192.144.128.229:2183",
                1000,
                event -> {
                    LogUtil.info("接收到事件：" + event);
                    if (Watcher.Event.KeeperState.SyncConnected == event.getState()) {
                        countDownLatch.countDown();
                    }
                });
        countDownLatch.await();
        String path1 = zookeeper.create("/zk-test2-ephemeral",
                "你好".getBytes(StandardCharsets.UTF_8),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL);
        LogUtil.info(path1);
        String path2 = zookeeper.create("/zk-test2-ephemeral",
                "你好".getBytes(StandardCharsets.UTF_8),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);
        LogUtil.info(path2);
        SleepUtils.seconds(10);
    }
}
